package edu.sdjzu.exam.mapper.exam;

import edu.sdjzu.exam.domain.exam.ExamQuestion;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ExamQuestionsMapper {

    @Insert("INSERT INTO exam_questions (exam_id, question_id, question_order, score) " +
            "VALUES (#{examId}, #{questionId}, #{questionOrder}, #{score})")
    void insertExamQuestion(ExamQuestion examQuestion);

    @Select("SELECT * FROM exam_questions WHERE exam_id = #{examId}")
    List<ExamQuestion> selectExamQuestionsByExamId(Long examId);

    @Select("SELECT * FROM exam_questions WHERE exam_id = #{examId} AND question_id = #{questionId}")
    ExamQuestion selectExamQuestionByExamAndQuestion(@Param("examId") Long examId, @Param("questionId") Long questionId);

    @Delete("DELETE FROM exam_questions WHERE exam_id = #{examId}")
    void deleteExamQuestionsByExamId(Long examId);

    @Select("SELECT COALESCE(SUM(score), 0) FROM exam_questions WHERE exam_id = #{examId}")
    BigDecimal calculateTotalPointsForExam(Long examId);

    @Select("SELECT COALESCE(SUM(score), 0) FROM exam_questions WHERE exam_id = #{examId}")
    BigDecimal calculateTotalScoreForExam(Long examId);
}
